

import { GameplayComponent } from "../../GameplayComponent";
import ViewGameplay from "../../ViewGameplay";
import { Comp } from "../../m/CompDef";
import { GpEcsWorld } from "../../m/GpEcsWorld";
import { GpEventDef } from "../../m/GpEventDef";
import { GpCmdSystem, GpCmdType, GpCmdDataDef } from "../../m/systems/CmdSystem";
import { HeroSystem } from "../../m/systems/HeroSystem";
import { GpEntity } from "../GpEntity";
import GpHero from "../GpHero";
import GpControlPanel from "./GpControlPanel";
import { ControlAction } from "./GpActionPanel";


import { _decorator, Component, Node, EventTouch } from 'cc';
import { BaseComponent } from "../../../../../extensions/cocos-scaffold-3x/assets/ui/base/BaseComponent";
import { gg } from "../../../../Game";
const { ccclass, property } = _decorator;



@ccclass
export default class GpActionTouchLayer extends BaseComponent<GpControlPanel>{

    ecsWorld: GpEcsWorld
    onInit(): void {
        console.log("onInit GpActionTouchLayer");

        this.ecsWorld = this.prop.prop.ecsWorld
        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
    }

    protected onDestroy(): void {

        this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
        gg.eventTarget.targetOff(this)
    }

    onTouchStart(event: EventTouch) {
        // console.log(event);

        if (this.prop.gpActionPanel.currentActionId == ControlAction.move || this.prop.gpActionPanel.currentActionId == ControlAction.jump) {
            event.target._touchListener.swallowTouches = true
        } else {
            event.target._touchListener.swallowTouches = false
        }
    }
    onTouchEnd(event: EventTouch) {
        // console.log(event);
        if (this.prop.gpActionPanel.currentActionId == ControlAction.move || this.prop.gpActionPanel.currentActionId == ControlAction.jump) {
            event.target._touchListener.swallowTouches = true
            this.prop.gpActionPanel.onTouchActionTouchLayer(event)
        } else {
            event.target._touchListener.swallowTouches = false
        }
    }
    onTouchCancel(event: EventTouch) {

    }


}